import 'dart:developer';

import 'package:bruno/bruno.dart';
import 'package:demo_flutter/api/itemApi.dart';
import 'package:demo_flutter/common/vo/items.dart';
import 'package:demo_flutter/pages/view/itemPage/addItem.dart';
import 'package:demo_flutter/pages/view/itemPage/itemCard.dart';
import 'package:flutter/material.dart';

/// 打卡组件
/// by DoDayum
class ItemPage extends StatefulWidget {
  const ItemPage({super.key});

  @override
  State<ItemPage> createState() => _ItemPageState();
}

class _ItemPageState extends State<ItemPage> {
  List<Widget> jobList = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("物品管理"),
        backgroundColor: const Color.fromARGB(255, 30, 150, 242),
        actions: [
          BrnSmallMainButton(
            title: "+",
            fontSize: 30,
            bgColor: const Color.fromARGB(255, 30, 150, 242),
            onTap: () {
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return const ItemClockEvent(id: null);
              }));
            },
          )
        ],
      ),
      body: FutureBuilder<List<Item>>(
        future: ItemApi().getItems(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            log("ConnectionState.waiting");
            return const Center(child: CircularProgressIndicator()); // 显示加载指示器
          } else if (snapshot.hasError) {
            log("${snapshot.error}");
            return Text('Error: ${snapshot.error}'); // 显示错误信息
          } else {
            List<Item> responseList = snapshot.data ?? [];
            for (Item item in responseList) {
              jobList.add(ItemCard(item: item));
            }
            return ListView(
              scrollDirection: Axis.vertical,
              children: jobList,
            );
          }
        },
      ),
    );
  }
}
